package com.kehutong.charge.admin;

import com.kehutong.charge.entity.MeterManage;
import com.kehutong.charge.service.MeterManageService;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.util.time.TimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @program: kht
 * @description: 仪表管理，包括水表、电表、天然气表
 * @author: joneron
 * @create: 2021/6/21
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/meter/manage")
public class MeterManageController {

    @Inject
    MeterManageService meterManageService;


    @ReqMapping("/page")
    @HasPermission("pay:charge:meter:manage:view")
    public Object page(JSONObject jsonObject) {
        return meterManageService.findPage(jsonObject);
    }

    @ReqMapping("/switchStatus")
    @HasPermission("pay:charge:meter:manage:view")
    public Object switchStatus(String id) {
        return meterManageService.switchStatus(id);
    }

    /**
     * 根据小区获取仪表列表导出excel
     * @param jsonObject
     * @return
     */
    @ReqMapping("/page/export")
    @HasPermission("pay:charge:meter:manage:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject){
        meterManageService.pageExport(req,resp,jsonObject);
    }

    /**
     * 电表水表燃气表导入模板生成
     * @param req
     * @param resp
     */
    @ReqMapping("/template/export")
    @HasPermission("pay:charge:meter:reading:view")
    public void templateExport(HttpServletRequest req, HttpServletResponse resp,Token token) throws Exception {
        meterManageService.templateExport(req,resp,token);
    }


    @ReqMapping("/list")
    @HasPermission("pay:charge:meter:manage:view")
    public Object list(JSONObject jsonObject) {
        return meterManageService.findList(jsonObject);
    }


    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:meter:manage:edit")
    @ReqMapping("/save")
    public Object save(MeterManage entity, Token token) throws Exception {
        return meterManageService.save(entity,token);
    }


    /**
     * 水电燃气仪表导入检查
     * @param jsonObject
     */
    @ReqMapping("/import/check")
    public Object importExcelCheck(JSONObject jsonObject){
        meterManageService.importExcelCheck(jsonObject);
        return Result.success();
    }


    /**
     * 水电燃气仪表导入
     * @param jsonObject
     * @param token
     */
    @ReqMapping("/import")
    public Object importExcel(JSONObject jsonObject, Token token) throws Exception {
        return meterManageService.importExcel(jsonObject,token);
    }



    @Bean(copy=true)
    @HasPermission("pay:charge:meter:manage:edit")
    @ReqMapping("/update")
    public Object update(MeterManage entity,Token token) throws Exception {

        entity.setEditTime(TimeUtil.timestamp());
        entity.setUpdateName(token.getUsername());

        return meterManageService.update(entity);
    }


    @HasPermission("pay:charge:meter:manage:edit")
    @ReqMapping("/delete")
    public Object delete(MeterManage entity) {

        return meterManageService.deleteById(entity.getId());
    }


    @ReqMapping("/get")
    public Object get(MeterManage entity) {

        return Result.success(entity);
    }


    @ReqMapping("/getObj")
    public Object getObj(JSONObject jsonObject) {

        return meterManageService.findMeterObj(jsonObject);
    }


    @ReqMapping("/statist")
    public Object statist(JSONObject jsonObject, Token token) throws Exception {

        return meterManageService.findMeterStatist(jsonObject);
    }


    /**
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @HasPermission("pay:charge:meter:manage:view")
    @ReqMapping("/statist/export")
    public void statistExport(HttpServletRequest req, HttpServletResponse resp,JSONObject jsonObject, Token token) throws Exception {
        meterManageService.downMeterStatist(req,resp,jsonObject);
    }

}
